package com.ysstech.reportworld.service.impl;

import com.ysstech.reportworld.entity.DataCustomer;
import com.ysstech.reportworld.mapper.DataCustomerMapper;
import com.ysstech.reportworld.service.DataCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基础数据对应客户信息 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-12-02
 */
@Service
public class DataCustomerServiceImpl extends ServiceImpl<DataCustomerMapper, DataCustomer> implements DataCustomerService {

    @Override
    public Map<String, List<String>> getDataCustomerMap() throws Exception {
        // 所属客户 code和数据，对应一个list
        List<DataCustomer> customerList = this.list();
        if (CollectionUtils.isEmpty(customerList)) {
            return null;
        }
        Map<String, List<String>> mapDataCustomer = new HashMap<>();
        for (DataCustomer customer : customerList) {
            if (null == customer || StringUtils.isBlank(customer.getDataId()) || StringUtils.isBlank(customer.getDataCode())) {
                continue;
            }
            String key = customer.getDataCode() + "\t" + customer.getDataId();
            if (mapDataCustomer.containsKey(key)) {
                List<String> list = mapDataCustomer.get(key);
                list.add(customer.getCustomerId());
                mapDataCustomer.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(customer.getCustomerId());
                mapDataCustomer.put(key, list);
            }
        }
        return mapDataCustomer;
    }
}
